package com.shuihu.liutang.redis;
/*
 * Help Information
 *
 * Create Date:2017/4/13
 *
 * zhangle
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;

public abstract class AbsFileImporter {

    private static final Logger LOG = LoggerFactory.getLogger(AbsFileImporter.class);

    private static final String VERSION = "VERSION";
    private static final String DEFAULT_VERSION = "1";
    private static final String KEY_SET = "KEY_SET_";

    protected String host;

    protected int port;

    protected String versionName;

    protected String version;

    protected String keySetName;

    protected String key;

    protected Jedis jedis;

    protected abstract String getPrefix();

    protected abstract void parseLine(String line);

    protected abstract String getKey();

    protected abstract void saveRecord(Pipeline pipelined);

    private void createVersionName() {
        versionName = getPrefix() + VERSION;
    }

    private void getNextVersion() {
        version = jedis.get(versionName);

        if (version == null || version.isEmpty()) {
            version = DEFAULT_VERSION;
        }
        int i1 = Integer.parseInt(version.replace("_",""));
        version = String.valueOf(i1 % 2 + 1);
    }

    private void createKeySet() {
        keySetName = getPrefix() + KEY_SET + version;
    }

    private void deleteData() throws IOException {
        Set<String> smembers = jedis.smembers(keySetName);
        Pipeline pipelined = jedis.pipelined();
        int n = 0;
        for (String m : smembers) {
            pipelined.del(m);
            n++;
            if (n % 10000 == 0) {
                pipelined.sync();
            }
        }
        pipelined.sync();
        pipelined.close();
    }

    private void saveKeyset(Pipeline pipelined) {
        pipelined.sadd(keySetName, key);
    }

    private void updateVersion() {
        jedis.set(versionName, "_" + version);
    }

    private void createKey() {
        key = getPrefix() + getKey() + "_" + version;
    }

    public void init(String host, String port) {
        this.host = host;
        init(host, Integer.parseInt(port));
    }

    public void init(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void importFile(File file) throws Exception {
        try {
            jedis = new Jedis(host, port);
            // 判断文件是否存在
            if (!file.exists()) {
                LOG.warn("file not exists.");
                return;
            }

            // 生成版本号名称
            createVersionName();

            // 获取翻牌版本信息
            getNextVersion();

            // 生成key_set名称
            createKeySet();

            // 删除数据
            deleteData();

            // 开始读取文件
            String line;
            Scanner in = new Scanner(file);
            Pipeline pipelined = jedis.pipelined();
            int n = 0;
            while (in.hasNext()) {
                line = in.nextLine();
                // 生成一条记录
                parseLine(line);
                // 生成key
                createKey();
                // 存入记录
                saveRecord(pipelined);
                // 存入key_set
                saveKeyset(pipelined);
                n++;
                if (n % 10000 == 0) {
                    pipelined.sync();
                }
            }
            // 循环结束
            pipelined.sync();
            pipelined.close();
            // 更新版本号到存储
            updateVersion();
        } finally {
            jedis.close();
        }
    }
}
